package cn.clwxy.gdz.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.clwxy.gdz.bo.OrderImformation;
import cn.clwxy.gdz.entity.Order;
import cn.clwxy.gdz.util.BaseDao;

public class OrderDaoImpl extends BaseDao implements OrderDao {
	@Override
	public List<Order> findOrderByDate(Date date) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Order> findOrderByTeacher(Integer teacherId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Order> findOrderByStudent(Integer studentId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer addOrder(Order order) {
		String sql = "INSERT INTO `order`(studentId,teacherId,price,orderBeginTime,studentcomment,teachercomment,isPay,profit,isPushToIndex,orderFinishTime)"
				+ "value(?,?,?,?,?,?,?,?,?,?)";
		Object obj[] = { order.getStudentId(), order.getTeacherId(),
				order.getPrice(), order.getOrderBeginTime(),
				order.getStudentcomment(), order.getTeachercomment(),
				order.getIsPay(), order.getProfit(), order.getIsPushToIndex(),
				order.getOrderFinishTime() };
		int result = super.update(sql, obj);
		if (result == 1) {
			String sqllast = "SELECT orderId FROM `order` ORDER BY orderId DESC LIMIT 1";
			Order order2 = new Order();
			order2 = super.querySingle(sqllast, new BeanHandler<Order>(
					Order.class));
			return order2.getOrderId();
		}
		return 0;
	}

	@Override
	public int deleteOrder(Integer orderId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateOrder(Order order) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Integer getLastOrderId() {
		String sql = "SELECT max(orderId) orderId from `order`";
		Order order = super.querySingle(sql,
				new BeanHandler<Order>(Order.class));
		return order.getOrderId();

	}

	@Override
	public OrderImformation findOderByOrderId(Integer OrderId) {
		String sql = "SELECT o.orderId,o.price,o.orderBeginTime,o.studentcomment,o.teachercomment, "
				+ "t.teacherRealName,t.teacherPhone,s.studentName,s.studentPhone,GROUP_CONCAT(sbj.subjectName) subjectName "
				+ "FROM  `order`  o  "
				+ "INNER JOIN teacher t ON t.teacherId=o.teacherId "
				+ "INNER JOIN student s ON s.studentId=o.studentId "
				+ "INNER JOIN ordersubjectrelative osi ON osi.orderId=o.orderId "
				+ "INNER JOIN subject sbj ON sbj.subjectId=osi.subjectId "
				+ "WHERE o.orderId=?";

		return super.querySingle(sql,new BeanHandler<OrderImformation>(OrderImformation.class), OrderId);
	}

	@Override
	public Integer findOrderBySIdTIdInitiator(Integer studentId,
			Integer teacherId, Integer initiator) {
		String sql="SELECT COUNT(orderId) FROM `order` WHERE studentId=? AND teacherId=? AND Initiator=? AND orderBeginTime=?";
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		return super.queryColumn(sql, new ScalarHandler(), studentId,teacherId,initiator,simpleDateFormat.format(new Date())).intValue();
	}
	
	
	
	
}
